﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using MyBind;
using Model;
using System.IO;

public partial class Communicate_BBSAddTopic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request["topId"] != null)
            {
                this.Submit1.Visible = false;
                DAL.bbsTopic dalTop = new DAL.bbsTopic();
                int topId = int.Parse(Request["topId"].ToString().Trim());
                Model.bbsTopic mdlTop = dalTop.GetModel(topId);

                this.txTitle.Value = mdlTop.title;
                this.HtmlEditor1_HtmlEditorContent.Value = mdlTop.content;
            }
            else
            {
                this.Button1.Visible = false;
            }

        }
    }

    //发帖
    protected void Submit1_ServerClick(object sender, EventArgs e)
    {
        Session["UserName"].ToString();
        if(Session["secId"]!=null && Session["UserName"]!=null)
        {
            if (this.txTitle.Value.Trim() == "" || this.HtmlEditor1_HtmlEditorContent.Value.Trim() == "")
            {

                this.lblErrorMsg.Text = "请填写完整信息!";
            }
            else
            {
                DAL.bbsTopic dalTop = new DAL.bbsTopic();
                Model.bbsTopic mdlTop = new Model.bbsTopic();
                mdlTop.title = this.txTitle.Value.Trim();
                mdlTop.content = this.HtmlEditor1_HtmlEditorContent.Value.ToString();
                
                //用户信息
                string UserName = Session["UserName"].ToString();
                Sqlselete slet=new Sqlselete();
                mdlTop.userId = int.Parse(slet.SelectEmpByUserName(UserName));

                string secId = Session["secId"].ToString();
                mdlTop.secId = int.Parse(secId);
                mdlTop.sendTime= DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                mdlTop.flag = 0;                
                int top_retuen = dalTop.AddNoReply(mdlTop);

                //bbsTAttachment
                DAL.bbsTAttachment dalTA = new DAL.bbsTAttachment();
                Model.bbsTAttachment mdlTA = new Model.bbsTAttachment();
                mdlTA.tpcId = top_retuen;
                bool suc = true;
                for (int i = 0; i < files.Count;i++ )
                {
                    mdlTA.fileUrl = files[i].ToString();
                    int ta_retuen = dalTA.Add(mdlTA);
                    if (top_retuen <= 1 && ta_retuen <= 1)
                    {
                        suc = false;
                    }
                }
                if (suc)
                {
                    WriteScript.Mesg(Response, "保存成功");
                    WriteScript.ChangeLoca(Response, "BBSListView.aspx");
                }

                //一定要清空
                files.Clear();
            }
        }
    }

    public static string src = "";
    public static ArrayList files=new ArrayList();
    //上传附件
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.PostedFile.ContentLength == 0)
        {
            src = "";
        }
        else
        {
                //上传文件
                string strFilePath = FileUpload1.PostedFile.FileName;
                FileInfo fi = new FileInfo(strFilePath);
                string Ext = fi.Name;

                src = Ext;
                string ServerPath = Server.MapPath("~");
                string strSeraPath = ServerPath + "\\upfiles\\" + src;
                FileUpload1.PostedFile.SaveAs(strSeraPath);
                
                files.Add(strSeraPath);
                this.lblMsg.Text = "上传成功"; 
        }
    }

    //修改帖子
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["secId"] != null && Request["topId"] != null)
        {
            string secId = Session["secId"].ToString();
            DAL.bbsTopic dalTop = new DAL.bbsTopic();
            int topId = int.Parse(Request["topId"].ToString().Trim());
            Model.bbsTopic mdlTop = new Model.bbsTopic();
            mdlTop.id = topId;
            mdlTop.title = this.txTitle.Value;
            mdlTop.content=this.HtmlEditor1_HtmlEditorContent.Value;

            //bbsTAttachment
            DAL.bbsTAttachment dalTA = new DAL.bbsTAttachment();
            Model.bbsTAttachment mdlTA = new Model.bbsTAttachment();
            mdlTA.tpcId = topId;
            bool suc = true;
            for (int i = 0; i < files.Count; i++)
            {
                mdlTA.fileUrl = files[i].ToString();
                int ta_retuen = dalTA.Add(mdlTA);
                if (dalTop.UpdateNoReply(mdlTop) <= 0 && ta_retuen <= 1)
                {
                    suc = false;
                }
            }
            if (suc)
            {
                WriteScript.Mesg(Response, "修改成功");
                WriteScript.ChangeLoca(Response, "BBSListView.aspx");
            }

            //清空数组
            files.Clear();
            
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        this.txTitle.Value = "";
        this.HtmlEditor1_HtmlEditorContent.Value = "";
    }

}
